package com.example.demo.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.ExcelUtil;
import com.example.demo.common.util.ResultUtil;
import com.example.demo.common.util.entity.ExcelConfig;
import com.example.demo.common.util.entity.Result;
import com.example.demo.common.util.entity.ResultEnum;
import com.example.demo.config.Methods;
import com.example.demo.modules.test.dto.classes.ClassDTO;
import com.example.demo.modules.test.dto.classes.ClassExportDTO;
import com.example.demo.modules.test.dto.classes.ClassImportDTO;
import com.example.demo.modules.test.dto.classes.ClassQueryDTO;
import com.example.demo.modules.test.entity.Class;
import com.example.demo.modules.test.dao.ClassMapper;
import com.example.demo.modules.test.entity.Teacher;
import com.example.demo.modules.test.service.IClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.modules.test.service.ITeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lmy
 * @since 2022-12-28
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements IClassService {

    @Resource
    private Methods methods;

    @Resource
    private ITeacherService iTeacherService;

    @Override
    public IPage<ClassDTO> listClass(Page page, ClassQueryDTO queryDTO) {
        return this.baseMapper.listClass(page, queryDTO);
    }

    @Override
    public ResultEnum importClass(String filePath) {
        String root = methods.getUserDir();
        List<ClassImportDTO> list = ExcelUtil.read(root + File.separator + filePath, ClassImportDTO.class, new ExcelConfig(null, 2));
        List<Class> insertList = new ArrayList<>();
        list.forEach(e->{
            Class aClass = new Class();
            BeanUtils.copyProperties(e, aClass);
            if(StringUtils.isNotEmpty(e.getLeaderName())) {
                QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Teacher::getName, e.getLeaderName());
                Teacher leader = iTeacherService.getOne(queryWrapper);
                if(Objects.nonNull(leader)){
                    aClass.setLeaderId(leader.getId());
                }
            }
            insertList.add(aClass);
        });
        this.saveBatch(insertList);
        return ResultEnum.SUCCESS;
    }

    @Override
    public Result exportClass(ClassQueryDTO queryDTO) {
        List<ClassExportDTO> list = this.baseMapper.exportClass(queryDTO);
        String timestamp = String.valueOf(System.currentTimeMillis());
        File exportFile = new File(methods.getUploadPath() + File.separator + timestamp);
        if(!exportFile.exists()){
            exportFile.mkdirs();
        }
        String path = exportFile.getPath() + File.separator + "班级数据导出.xlsx";
        ExcelUtil.write(path, ClassExportDTO.class, new ExcelConfig("班级数据", null), list);
        return ResultUtil.success("upload" + File.separator + timestamp + File.separator + "班级数据导出.xlsx");
    }
}
